package com.catcher.infobackup.xyf;

import android.content.Context;
import android.os.Environment;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.os.storage.VolumeInfo;
import android.util.Log;



import java.io.File;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

import com.catcher.infobackup.R;

/**
 * Created by LiWeichao on 2016/9/15.
 */
public class StorageVolumnHelper {

    private static StorageVolume[] sStorageVolumes;

    public static synchronized StorageVolume[] getStorageVolumes(Context ctx, boolean reload) {
        if (sStorageVolumes == null || reload) {
//IMP!! Android SDK doesn't have a "getVolumeList" but is supported by CM10.
//Use reflect to get this value (if possible)
            try {
                StorageManager sm = (StorageManager) ctx.getSystemService(Context.STORAGE_SERVICE);
                Method method = sm.getClass().getMethod("getVolumeList"); //$NON-NLS-1$
                sStorageVolumes = (StorageVolume[]) method.invoke(sm);

            } catch (Exception ex) {
//Ignore. Android SDK StorageManager class doesn't have this method
//Use default android information from environment
                try {
                    File externalStorage = Environment.getExternalStorageDirectory();
                    String path = externalStorage.getCanonicalPath();
                    String description = null;
                    if (path.toLowerCase(Locale.ROOT).indexOf("usb") != -1) { //$NON-NLS-1$
                        description = ctx.getString(R.string.udisk);
                    } else {
                        description = ctx.getString(R.string.storage_card);
                    }
                } catch (Exception ex2) {
/**NON BLOCK**/
                }
            }
            if (sStorageVolumes == null) {
                sStorageVolumes = new StorageVolume[]{};
            }
        }
        return sStorageVolumes;
    }

    public static String getUSBpath(Context ctx) {
        StorageManager mStorageManager = (StorageManager) ctx.getSystemService(Context.STORAGE_SERVICE);
        final List<VolumeInfo> volumes = mStorageManager.getVolumes();
        Collections.sort(volumes, VolumeInfo.getDescriptionComparator());

        Log.w("StorageSettings", "StorageSettings, refresh, volumes.size() == " + volumes.size());
        for (VolumeInfo vol : volumes) {

            if (vol.getType() == VolumeInfo.TYPE_PUBLIC) {
                Log.w("StorageSettings", "StorageSettings, refresh, external vol.getPath() == " + vol.getPath());
                if (vol.getDisk().isUsb()) {
                    if (vol.getPath() == null) {
                        return "null";
                    }
//                    Utils.PATH_UDISK = vol.getPath().getPath();
//                    Constants.U_FILE_FLAG = Utils.PATH_UDISK;
                    StorageUtil.setUDiskPath(vol.getPath().getPath());
                    StorageUtil.setUDiskState(getState(ctx, vol.getPath().getPath()).equals("mounted"));
                    //Log.w("liweichao", "Utils.PATH_udisk == " + Utils.PATH_UDISK);
                    //Log.w("liweichao", "Utils.PATH_udisk state is  " + mStorageManager.getVolumeState(Utils.PATH_UDISK));
                    return StorageUtil.getUdiskPath();
                }
            }
        }
        return "null";
    }

    public static String getTFCardPath(Context ctx) {
        StorageManager mStorageManager = (StorageManager) ctx.getSystemService(Context.STORAGE_SERVICE);
        final List<VolumeInfo> volumes = mStorageManager.getVolumes();
        Collections.sort(volumes, VolumeInfo.getDescriptionComparator());

        Log.w("StorageSettings", "StorageSettings, refresh, volumes.size() == " + volumes.size());
        for (VolumeInfo vol : volumes) {

            if (vol.getType() == VolumeInfo.TYPE_PUBLIC) {
                Log.w("StorageSettings", "StorageSettings, refresh, external vol.getPath() == " + vol.getPath());
                if (vol.getDisk().isSd()) {
                    if (vol.getPath() == null) {
                        return "";
                    }
                    String tfpath = vol.getPath().getPath();
                    StorageUtil.setTFCardPath(tfpath);
                    StorageUtil.setTFCardState(getState(ctx, tfpath).equals("mounted"));
                    Log.w("liweichao", "Utils.PATH_udisk state is  " + mStorageManager.getVolumeState(vol.getPath().toString()));

                    return vol.getPath().getPath();
                }
            }
        }
        return "";
    }

    public static String getState(Context ctx, String path) {
        if (ctx == null || path == null || path.equals(""))
            return "unknown";
        StorageManager mStorageManager = (StorageManager) ctx.getSystemService(Context.STORAGE_SERVICE);
        return mStorageManager.getVolumeState(path);
    }

    public static void setUSBpath(Context ctx) {
        StorageManager mStorageManager = (StorageManager) ctx.getSystemService(Context.STORAGE_SERVICE);

        final List<VolumeInfo> volumes = mStorageManager.getVolumes();
        Collections.sort(volumes, VolumeInfo.getDescriptionComparator());

        Log.w("StorageSettings", "StorageSettings, refresh, volumes.size() == " + volumes.size());
        for (VolumeInfo vol : volumes) {

            if (vol.getType() == VolumeInfo.TYPE_PRIVATE) {

                Log.w("StorageSettings", "StorageSettings, refresh, internal vol.getPath() == " + vol.getPath());
            } else if (vol.getType() == VolumeInfo.TYPE_PUBLIC) {
                Log.w("StorageSettings", "StorageSettings, refresh, external vol.getPath() == " + vol.getPath());
//                Log.w("StorageSettings", "StorageSettings, refresh, external vol.getDisk().isSd() == " + vol.getDisk().isSd());
//                Log.w("StorageSettings", "StorageSettings, refresh, external vol.getDisk().isUsb() == " + vol.getDisk().isUsb());
                if (vol.getDisk().isSd()) {
                  
                    Log.w("StorageSettings", "Utils.PATH_TFCARD == " +vol.getPath().getPath());
                } else if (vol.getDisk().isUsb()) {
                   
                    Log.w("StorageSettings", "Utils.PATH_TFCARD == " + vol.getPath().getPath());
                }
            }
        }

    }
}